<template>
	<u-modal :show="showShare" :showConfirmButton="false" width="750rpx">
		<view class="closeBtn cuIcon-close" @click="showShare = false"></view>
		<view class="slot-content" style="background-color: rgba(0,0,0,0);">
			<view style="position: relative; width: 650rpx;">
				<!-- <image style="width: 550rpx; height: 977rpx;" mode="aspectFill" src="../static/share.jpg" ></image> -->
				<!-- <image style="position: absolute; width:250rpx; height: 250rpx; top: 360rpx; left: 50%; transform: translateX(-50%);" :src="qrPath" mode="aspectFit"></image> -->
				<image style="width: 650rpx; height: 800rpx;" :src="qrPath" mode="aspectFit"></image>
			</view>
			<view class="flex padding" style="gap: 20rpx;">
				<!-- <u-button id="shareBtn" type="primary" open-type="share" plain icon="share-fill">转发</u-button> -->
				<!-- <button id="shareBtn" open-type="share">转发</button> -->
				<u-button type="primary" icon="photo-fill" @click="saveImg">分享</u-button>
			</view>

		</view>
	</u-modal>
</template>

<script>
	import loginApi from '../api/new/login'
	export default {
		props: {
			url: {type: String},
			scene: { type: [Object, String] },
			type: {type: String}
		},
		// onShareAppMessage(res) {
		// 	  console.log("分享按钮回调11111", res)
		// 	  console.log("分享的URL", this.url)
		// 	  console.log("分享的scene", this.scene)
		//   // 发送给朋友
		//   return {
		//     // title: "your title", 
		//     // imageUrl: "your image",
		// 	  path: this.url,
		// 	  scene: JSON.stringify(this.scene)
		//   };
		// },
		// onShareTimeline(res) {
		//   // 分享到朋友圈
		//   return {
		//     // title: "your title",
		//     // imageUrl: "your image",
		// 	path: this.url,
		// 	scene: JSON.stringify(this.scene)
		//   };
		// },
		data() {
			return {
				showShare: false,
				qrPath: "",
				imgurl: ""
			}
		},
		methods: {
			show() {
				this.showShare = true
				this.qrPath = ""
				this.$nextTick(() => {
					this.createQrCode()
				})
			},
			createQrCode() {
				console.log("uurl", this.url)
				console.log("sscene", this.scene)
				this.userInfo = uni.getStorageSync("userInfo")
				if (['shareProduct'].includes(this.type)) {
					loginApi.shareRQcode({
						page: uni.getStorageSync("url"),
						scene: uni.getStorageSync("scene")
					}).then(res => {
						this.qrPath = "data:image/png;base64," + res.data.images
						this.imgurl = res.data.url
					})
				} else if (['invite'].includes(this.type)){
					loginApi.getQRcode({
						page: uni.getStorageSync("url"),
						scene: uni.getStorageSync("scene")
						// scene: JSON.stringify(uni.getStorageSync("scene"))
					}).then(res => {
						console.log(res)
						this.qrPath = "data:image/png;base64," + res.data.images
						this.imgurl = res.data.url
						// this.qrPath = res.data.path
					})
				}
			},
			saveImg() {
				let that = this
				
				wx.downloadFile({
				   url: this.imgurl,
				   success: (res) => {
				     wx.showShareImageMenu({
				       path: res.tempFilePath //要分享的图片地址，必须为本地路径或临时路径
				     })
				   }
				 })
				
				//获取授权列表，查看是否授权写入相册权限
				// uni.getSetting({
				// 	success(res) {
				// 		if (!res.authSetting['scope.writePhotosAlbum']) {
				// 			uni.authorize({
				// 				scope: 'scope.writePhotosAlbum',
				// 				success() {
				// 					that.saveimgtoAlbum(that.imgurl)
				// 				}
				// 			})
				// 		} else {
				// 			that.saveimgtoAlbum(that.imgurl)
				// 		}
				// 	}
				// })
			},
			saveimgtoAlbum(imgurl) {
				//图片保存到本地s
				console.log("保存图片到本地")
				let that = this
				uni.showLoading()
				uni.saveImageToPhotosAlbum({
					filePath: that.imgurl,
					success: function(data) {
						uni.hideLoading()
						uni.showToast({
							title: '保存成功',
							icon: 'success',
							duration: 2000
						})
					},
					fail: function(err) {
						uni.hideLoading()
						if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
							uni.openSetting({
								success(settingdata) {
									if (settingdata.authSetting['scope.writePhotosAlbum']) {
										uni.showToast({
											title: '您已授权成功，请重新保存海报',
											icon: 'success',
											duration: 2000
										})
									} else {
										uni.showToast({
											title: '尚未授权，无法保存海报',
											icon: 'none',
											duration: 2000
										})
									}
								}
							})
						}
					},
					complete(res) {
						console.log(res);
					}
				})

			}
		}
	}
</script>

<style lang="scss">
	.closeBtn{
		position: absolute;
		right: 50rpx;
		top: 50rpx;
		z-index: 999;
	}
</style>